import utils from '../core/utils';
import logger from '../core/logger';
import apiInfo from './core/apiInfo';
import dataInfo from './core/dataInfo';
import apiProvider from './core/apiProvider';
import { clientBase } from './core/clientBase';
import returnModel from '../model/common/returnModel';
import watchlaterItemModel from '../model/other/watchlaterItemModel';

class watchLaterClient extends clientBase {
  constructor() {
    super();
  }

  add(id: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let info = new dataInfo(`aid=${id}`, apiProvider.Build6);
      this.client.postResult("https://api.bilibili.com/x/v2/history/toview/add", info).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>());
          else {
            var failed = self.buildFailedResult<any>(res.toString());
            resolve(failed);
          }
        } catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  list(pageIndex: number): Promise<returnModel<Array<watchlaterItemModel>>> {
    var self = this;
    return new Promise((resolve) => {
      let info = new apiInfo("https://api.bilibili.com/x/v2/history/toview", `&pn=${pageIndex}&ps=100`, null, true);
      info.buildVer = apiProvider.Build6;
      this.client.getResult(info).then((res) => {
        try {
          var array = res.data.list;
          resolve(self.buildSuccessResult<Array<watchlaterItemModel>>(array));
        } catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<Array<watchlaterItemModel>>(e));
        }
      });
    });
  }

  clear(): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let info = new dataInfo('', apiProvider.Build6);
      this.client.postResult("https://api.bilibili.com/x/v2/history/toview/clear", info).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>());
          else {
            var failed = self.buildFailedResult<any>(res.toString());
            resolve(failed);
          }
        } catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }

  del(id?: string): Promise<returnModel<any>> {
    var self = this;
    return new Promise((resolve) => {
      let info: dataInfo;
      if (utils.isEmpty(id))
        info = new dataInfo(`viewed=true`, apiProvider.Build6);
      else
        info = new dataInfo(`aid=${id}`, apiProvider.Build6);
      this.client.postResult("https://api.bilibili.com/x/v2/history/toview/del", info).then((res) => {
        try {
          if (res.code == 0)
            resolve(self.buildSuccessResult<any>());
          else {
            var failed = self.buildFailedResult<any>(res.toString());
            resolve(failed);
          }
        } catch (e) {
          logger.error(e);
          resolve(self.buildExceptionResult<any>(e));
        }
      });
    });
  }
}

export default new watchLaterClient();